package cn.anjubao.cp.api.pay;

import cn.anjubao.cp.api.common.BussinessType;
import cn.anjubao.cp.api.common.Constants;
import cn.anjubao.cp.api.dto.CountFeeReqForm;
import cn.anjubao.cp.api.dto.CountFeeResDto;
import cn.anjubao.cp.api.dto.unifiedPayReqForm;
import cn.anjubao.cp.api.entity.ParkingRecord;
import cn.anjubao.cp.api.entity.TradeOrder;
import cn.anjubao.cp.api.feign.ParkingLotFeignClient;
import cn.anjubao.cp.api.mapper.ParkingRecordMapper;
import cn.anjubao.cp.api.pay.utils.PayUtils;
import cn.anjubao.cp.common.base.exception.BusinessException;
import cn.anjubao.cp.common.utils.DateHelper;
import com.anjubao.parking.municipal.common.dto.ApiResult;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingTotalFeeDto;
import com.anjubao.parking.municipal.payment.dto.PayResponseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class TradeOrderService {

    @Autowired
    private PaymentService paymentService;
    @Autowired
    private PayUtils payUtils;
    @Autowired
    private ParkingLotFeignClient parkingLotFeignClient;
    @Autowired
    private ParkingRecordMapper parkingRecordMapper;


    public PayResponseDto unifiedPay(unifiedPayReqForm reqForm) throws BusinessException {
        PayResponseDto payResponseDto=null;
        String orderId=reqForm.getUserCode()+ BussinessType.PARKING.value+System.currentTimeMillis();
        if (reqForm.getPayType()==1) {
            orderId=reqForm.getUserCode()+ BussinessType.BALANCE.value+System.currentTimeMillis();
        }
        TradeOrder tradeOrder=new TradeOrder();
        tradeOrder.setId(orderId);
        tradeOrder.setUserId(reqForm.getUserCode());
        tradeOrder.setAmount(reqForm.getAmount());
        tradeOrder.setOrderState(0);
        //一.余额支付
        if (reqForm.getPaySource()==1){
            tradeOrder.setPaySource(1);
            //1.1低杆缴费
            if (reqForm.getPayType()==2){
                tradeOrder=payUtils.packTradeOrder(reqForm,tradeOrder, Constants.lowPolePay);
                paymentService.blancePay(reqForm,tradeOrder,Constants.lowPolePay);
            }
            //1.2中杆预缴
            if (reqForm.getPayType()==3){
                tradeOrder=payUtils.packTradeOrder(reqForm,tradeOrder, Constants.middlePolePrePay);
                paymentService.blancePay(reqForm,tradeOrder,Constants.middlePolePrePay);
            }
            //1.3中杆补缴
            if (reqForm.getPayType()==4){
                tradeOrder=payUtils.packTradeOrder(reqForm,tradeOrder, Constants.middlePoleFixPay);
                paymentService.blancePay(reqForm,tradeOrder,Constants.middlePoleFixPay);
            }
        }

        //二.在线支付
        if (reqForm.getPaySource()==2){
            tradeOrder.setPaySource(2);
            //1.1低杆缴费
            if (reqForm.getPayType()==2){
                tradeOrder=payUtils.packTradeOrder(reqForm,tradeOrder, Constants.lowPolePay);
                payResponseDto=paymentService.onLinePay(reqForm,tradeOrder,Constants.lowPolePay);
            }
            //1.2中杆预缴
            if (reqForm.getPayType()==3){
                tradeOrder=payUtils.packTradeOrder(reqForm,tradeOrder, Constants.middlePolePrePay);
                payResponseDto=paymentService.onLinePay(reqForm,tradeOrder,Constants.middlePolePrePay);
            }
            //1.3中杆补缴
            if (reqForm.getPayType()==4){
                tradeOrder=payUtils.packTradeOrder(reqForm,tradeOrder, Constants.middlePoleFixPay);
                payResponseDto=paymentService.onLinePay(reqForm,tradeOrder,Constants.middlePoleFixPay);
            }
            //1.4余额充值
            if (reqForm.getPayType()==1){
                tradeOrder.setOrderType(1);
                tradeOrder.setPaySource(2);
                payResponseDto=paymentService.onLinePay(reqForm,tradeOrder,Constants.rechargePay);
            }
        }
        return payResponseDto;
    }


    public CountFeeResDto countFee(CountFeeReqForm reqForm) throws BusinessException {
        ParkingRecord parkingRecord=parkingRecordMapper.getRecordBySlotNum(reqForm.getSlotNum());
        if (parkingRecord==null){
            throw new BusinessException("-1","该车位无进场车辆!");
        }
        Date startTime= parkingRecord.getInTime();
        Date endTime= new Date(startTime.getTime() + reqForm.getParkTime()*1000);
        ApiResult<ParkingTotalFeeDto>  apiResult=parkingLotFeignClient.getParkingTotalFeeWithString(parkingRecord.getParkingLotCode(),
                DateHelper.getDateString(startTime), DateHelper.getDateString(endTime));
        if (apiResult.getCode()!=2000){
            throw new BusinessException("-1",apiResult.getDesc());
        }
        int parkingFee=apiResult.getResult().getTotalFee();
        int monitorFee=0;
        if (reqForm.getEnableMonitor()){
            monitorFee=((reqForm.getParkTime() - 1)/Constants.monitor_time_section + 1) * Constants.monitor_Fee_section;
        }
        CountFeeResDto resDto=new CountFeeResDto();
        resDto.setParkingFee(parkingFee);
        resDto.setMonitorFee(monitorFee);
        return resDto;
    }


}
